<template>
  <div class="suggestBox">
    <textarea class="no-style" v-model="content" placeholder="请输入您的宝贵意见"></textarea>
    <div class="buttons">
      <button class="primary" @click="saveBase($event)">提交</button>
    </div>
    <v-loading :showLoading="showLoading"></v-loading>
  </div>
</template>
<script>
  import {HttpRequest} from '../../common/js/httpFun'
  import {linklist} from '../../common/js/mockdata'
  import {ctoast, confirmDialog, AlertDialog} from '../../common/js/common'
  import Loading from '../loading/index'
  export default ({
    data () {
      return {
        'content': '',
        'showLoading': false
      }
    },
    mounted () {

    },
    computed: {},
    methods: {
      createToast (text) {
        ctoast(text)
      },
      createConfirmDialog (msg, yesFun, noFun) {
        confirmDialog(msg, yesFun, noFun)
      },
      saveBase (event) {
        event.preventDefault()
        event.stopPropagation()
        // 验证规则
        if (!this.content) {
          AlertDialog('提示', '请填写建议')
          return false
        }
        else {
          if (this.content.length > 150) {
            AlertDialog('提示', '最多输入150个字')
            return false
          }
          else if (this.content.length < 5) {
            AlertDialog('提示', '意见至少5个字')
            return false
          }
        }
        this.createConfirmDialog('确定提交意见反馈吗?', () => {
          this.showLoading = true
          let result = HttpRequest(linklist.useSuggestionUrl, {content: this.content}, 2)
          result.then((response) => {
            let body = response.body
            this.showLoading = false
            if (body.code === 0) {
              this.$emit('saveBase')
            }
            else {
              AlertDialog('提示', body.msg)
            }
          }, (response) => {
            this.showLoading = false
            AlertDialog('提示', '保存用户信息失败，请稍后再试')
          }).catch((response) => {
            this.showLoading = false
            AlertDialog('提示', '保存用户信息失败，请稍后再试')
          })
        })
      }
    },
    components: {
      'v-loading': Loading
    }
  })
</script>
